#include "StdAfx.h"
#include "MessageFactory.h"

MessageFactory::MessageFactory(void)
{
}

MessageFactory::~MessageFactory(void)
{
}

UnitTest MessageFactory::Create(const testing::UnitTest& unitTest) const
{
    UnitTest unitTestMsg;

    unitTestMsg.set_originalworkingdir(unitTest.original_working_dir());
    unitTestMsg.set_successfultestcasecount(unitTest.successful_test_case_count());
    unitTestMsg.set_failedtestcasecount(unitTest.failed_test_case_count());
    unitTestMsg.set_totaltestcasecount(unitTest.total_test_case_count());
    unitTestMsg.set_testcasetoruncount(unitTest.test_case_to_run_count());
    unitTestMsg.set_successfultestcount(unitTest.successful_test_count());
    unitTestMsg.set_failedtestcount(unitTest.failed_test_count());
    unitTestMsg.set_disabledtestcount(unitTest.disabled_test_count());
    unitTestMsg.set_totaltestcount(unitTest.total_test_count());
    unitTestMsg.set_testtoruncount(unitTest.test_to_run_count());
    unitTestMsg.set_elapsedtime(unitTest.elapsed_time());
    unitTestMsg.set_passed(unitTest.Passed());
    unitTestMsg.set_failed(unitTest.Failed());

    if (unitTest.current_test_case() != NULL) {
        unitTestMsg.mutable_currenttestcase()->CopyFrom(Create(*(unitTest.current_test_case())));
    }
    if (unitTest.current_test_info() != NULL) {
        unitTestMsg.mutable_currenttestinfo()->CopyFrom(Create(*(unitTest.current_test_info())));
    }

    for (int i = 0; i < unitTest.total_test_case_count(); ++i) {
        unitTestMsg.add_testcase()->CopyFrom(Create(*(unitTest.GetTestCase(i))));
    }

    return unitTestMsg;
}

TestCase MessageFactory::Create(const testing::TestCase& testCase) const
{
    TestCase testCaseMsg;

    testCaseMsg.set_name(testCase.name());
    testCaseMsg.set_comment(testCase.comment());
    testCaseMsg.set_successfultestcount(testCase.successful_test_count());
    testCaseMsg.set_failedtestcount(testCase.failed_test_count());
    testCaseMsg.set_disabledtestcount(testCase.disabled_test_count());
    testCaseMsg.set_totaltestcount(testCase.total_test_count());
    testCaseMsg.set_passed(testCase.Passed());
    testCaseMsg.set_failed(testCase.Failed());
    testCaseMsg.set_elapsedtime(testCase.elapsed_time());

    for (int i = 0; i < testCase.total_test_count(); ++i) {
        testCaseMsg.add_testinfo()->CopyFrom(Create(*(testCase.GetTestInfo(i))));
    }

    return testCaseMsg;
}

TestInfo MessageFactory::Create(const testing::TestInfo& testInfo) const
{
    TestInfo testInfoMsg;

    testInfoMsg.set_testcasename(testInfo.test_case_name());
    testInfoMsg.set_name(testInfo.name());
    testInfoMsg.set_testcasecomment(testInfo.test_case_comment());
    testInfoMsg.set_comment(testInfo.comment());
    testInfoMsg.mutable_result()->CopyFrom(Create(*(testInfo.result())));

    return testInfoMsg;
}

TestResult MessageFactory::Create(const testing::TestResult& testResult) const
{
    TestResult testResultMsg;

    testResultMsg.set_totalpartcount(testResult.total_part_count());
    testResultMsg.set_passed(testResult.Passed());
    testResultMsg.set_failed(testResult.Failed());
    testResultMsg.set_hasfatalfailure(testResult.HasFatalFailure());
    testResultMsg.set_hasnonfatalfailure(testResult.HasNonfatalFailure());
    testResultMsg.set_elapsedtime(testResult.elapsed_time());

    for (int i = 0; i < testResult.total_part_count(); ++i) {
        testResultMsg.add_testpartresult()->CopyFrom(Create(testResult.GetTestPartResult(i)));
    }

    for (int i = 0; i < testResult.test_property_count(); ++i) {
        testResultMsg.add_testproperty()->CopyFrom(Create(testResult.GetTestProperty(i)));
    }

    return testResultMsg;
}

TestPartResult MessageFactory::Create(const testing::TestPartResult& testPartResult) const
{
    TestPartResult testPartResultMsg;
    testing::TestPartResult::Type type = testPartResult.type();

    if (type == testing::TestPartResult::kSuccess) {
        testPartResultMsg.set_type(TestPartResult::SUCCESS);
    }
    else if (type == testing::TestPartResult::kNonFatalFailure) {
        testPartResultMsg.set_type(TestPartResult::NON_FATAL_FAILURE);
    }
    else if (type == testing::TestPartResult::kFatalFailure) {
        testPartResultMsg.set_type(TestPartResult::FATAL_FAILURE);
    }

    testPartResultMsg.set_filename(testPartResult.file_name());
    testPartResultMsg.set_linenumber(testPartResult.line_number());
    testPartResultMsg.set_summary(testPartResult.summary());
    testPartResultMsg.set_message(testPartResult.message());

    return testPartResultMsg;
}

TestProperty MessageFactory::Create(const testing::TestProperty& testProperty) const
{
    TestProperty testPropertyMsg;

    testPropertyMsg.set_key(testProperty.key());
    testPropertyMsg.set_value(testProperty.value());

    return testPropertyMsg;
}
